package com.recharge.srevice;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import javax.transaction.Transactional;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.entity.Customer;
import com.entity.CustomerMeter;
import com.entity.CustomerMeterPK;
import com.entity.Recharge;
import com.file.entity.FileCustomer;
import com.file.service.CustomerService;
import com.interfaces.CustomerDao;
import com.interfaces.CustomerMeterDao;
import com.interfaces.RechargeDao;
import com.utils.JsonResult;
import com.utils.Utils;

/**
 * @Title: RechargeServive.java
 * @Package com.recharge.srevice
 * @author 孙梦楠
 * @date 2017年8月22日 下午1:21:01
 * @version V1.0.0
 */

@Service
public class OpenAccountService {
	
	private static final Logger log = LoggerFactory.getLogger(OpenAccountService.class);


	@Autowired
	private RechargeDao rechargedao;

	@Autowired
	private CustomerDao customerdao;

	@Autowired
	private CustomerService customerservice;
	
	@Autowired
	private CustomerMeterDao customermeterdao;

	/*-------------开户-------------*/

	// 列出所有可以开户的用户
	@Transactional
	public JsonResult<List<FileCustomer>> listallcustomer(String opeorgcookie, String opetypecookie,String customeraccount,
			String customername, String customeraddress,String languagecookie,String childtotalstatus) {

		return customerservice.listallcustomer(opeorgcookie,opetypecookie, "2", "1", 461, "列出所有可开户用户", customeraccount, customername,
				customeraddress,languagecookie,childtotalstatus);
	}

	// 开户操作
	@Transactional
	public JsonResult<Recharge> openaccount(Integer customerid, String opecodecookie) {

		if ( Utils.isNullOrEmpty(opecodecookie)
				|| customerid == null ) {

			return new JsonResult<Recharge>(463, "开户失败..数值为空", null);
		} else {

			Recharge recharge = new Recharge();
			// 设置用户基本信息
			Customer customer = customerdao.findOne(customerid);
			recharge.setCustomerid(customerid);
			recharge.setCustomeraccount(customer.getCustomeraccount());
			recharge.setCustomername(customer.getCustomername());
			recharge.setCustomeraddress(customer.getCustomeraddress());
			recharge.setCustomerorg(customer.getCustomerorg());
			recharge.setCustomertype(customer.getCustomertype());
			//设置表信息
			
			Integer meterid = customermeterdao.findByCustomerid2(customerid).getPk().getMeterid();
			recharge.setMeterid(meterid);
			// 购电 第0次 为开户
			recharge.setDirection("购电");
			recharge.setFrequency(0);
			// 操作员
			recharge.setOpecode(opecodecookie);
			// 命令下发时间 获取当前系统时间
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			String issuetime = sdf.format(new Date());
			recharge.setIssuetime(issuetime);
			// 设置金额
			recharge.setBeforemoney(0.00);
			recharge.setMoney(0.00);
			recharge.setAftermoney(0.00);
			recharge.setTotalmoney(0.00);
			// 设置工单
			recharge.setOptstatus("等待下发");
			recharge.setOptnum(0);
			recharge.setPrintnum(0);
			// 其余在开户时为null
			// 更改用户账户状态
			customerdao.updateCustomer_recharge("2", customerid);
			// 保存
			rechargedao.save(recharge);
			

			return new JsonResult<Recharge>(462, "开户成功", recharge);
		}

	}
	
//		// 开户数据库操作
//		@Transactional
//		public void openAccount_db(Integer  meterid) {
//			Integer customerid = customermeterdao.findByMeterid(meterid).getPk().getCustomerid();
//			// 更改用户账户状态
//			customerdao.updateCustomer_recharge("2", customerid);
//		}
	
	
	
	
}
